/*
 * fLabel.cpp
 *
 *  Created on: 2011-3-17
 *      Author: 
 */

#include "YuebaoLabel.h"
#include "ProjectsDefines.h"
#include "BetterUserHBufC.h"
#include "MagazineDefines.h"
fLabel* fLabel::NewLC( fEngine* aEngine )
{
    fLabel* self = new ( ELeave ) fLabel( );
    CleanupStack::PushL( self );
    self->ConstructL( aEngine );
    return self;
}

fLabel* fLabel::NewL( fEngine* aEngine )
{
    fLabel* self = fLabel::NewLC( aEngine );
    CleanupStack::Pop( ); // self;
    return self;
}

fLabel::fLabel( )
{

}

fLabel::~fLabel( )
{
    SAFE_DELETE( _content );
}

void fLabel::ConstructL( fEngine* aEngine )
{
    BaseInit( aEngine );

    _font = fFont::get_device_font( kPanelTitleFontHeight, EFalse, EFalse );
    _text_color = 0x00;
    _text_align = ECenter;
}

void fLabel::SetRect( const TRect& aRect )
{
    SetCtlPos( aRect.iTl.iX, aRect.iTl.iY );
    SetCtlSize( aRect.Size( ).iWidth, aRect.Size( ).iHeight );
}

void fLabel::Layout( )
{
    _ASSERT_LOG( Width() != 0 );
    _ASSERT_LOG( Height() != 0 );

    if( _content && _content->Length( ) > 0 )
    {
        const int margin = 2;
        int text_x = Left( );
        int text_y = Top( );
        int text_width = _font->TextWidthInPixels( _content->Des( ) );

        _text_x_offset = margin;
        _text_y_offset = ( Height( ) - _font->HeightInPixels( ) ) >> 1;
        if( _text_y_offset < 0 )
        {
            _text_y_offset = 0;
        }
        const int areaWidth = Width( ) - margin - margin;
        if( _text_align == ECenter )
        {
            if( text_width < areaWidth )
            {
                _text_x_offset = ( areaWidth - text_width ) >> 1;
            }
        }
        else if( _text_align == ERight )
        {
            if( text_width < areaWidth )
            {
                _text_x_offset = Width( ) - margin - text_width;
            }
        }
        else
        {

        }

        _splashString.Update( areaWidth, text_width );

    }
}

void fLabel::RenderFrame( )
{
    _ASSERT_LOG( Width() != 0 );
    _ASSERT_LOG( Height() != 0 );

    if( _content && _content->Length( ) > 0 )
    {
        if( !_layouted )
        {
            Layout( );
            _layouted = true;
        }

        int text_x = Left( );
        int text_y = Top( );

        _splashString.Draw( *iGfx, 1, text_x + _text_x_offset, text_y + _text_y_offset, _text_color, _font, _content->Des( ), false );
    }
}

void fLabel::HandleKey( f_KEY_DES* /*aKeyDes*/)
{

}

TInt fLabel::HandleMouse( fMouseEvent /*nType*/, TPoint /*sPoint*/)
{
    return 0;
}
void fLabel::SetCotent( const TDesC& content )
{
    BetterUtility::Copy( _content, content );
    _layouted = false;
}

void fLabel::SetFontSize( int size )
{
    _font = fFont::get_device_font( size, EFalse, EFalse );
    _layouted = false;
}

